package com.gukun.springboot.tdengine.service.highvolume;

import cn.hutool.core.date.DateTime;
import com.gukun.springboot.tdengine.domain.enums.TagType;
import com.gukun.springboot.tdengine.pojo.TagData;
import com.gukun.springboot.tdengine.service.highvolume.pojo.DemoTagData;

import java.util.Iterator;
import java.util.List;

/**
 * Generate test data
 */
class MockDataSource implements Iterator {
    private long maxRowsPerTable = 1000000000L;

    // 100 milliseconds between two neighbouring rows.
    long startMs = System.currentTimeMillis() - maxRowsPerTable * 100;
    private int currentRow = 0;

    private List<DemoTagData> tagDataList;

    private int currentTbId = -1;

    public MockDataSource(List<DemoTagData> tlist) {
        this.tagDataList = tlist;
    }

    @Override
    public boolean hasNext() {
        currentTbId += 1;
        if (currentTbId == tagDataList.size()) {
            System.out.println("加完了："+currentTbId + "--" + DateTime.now());
            currentTbId = 0;
        }
        return currentRow < maxRowsPerTable;
    }

    @Override
    public String next() {
//        long ts = startMs + 100 * currentRow;
//        int groupId = currentTbId % 5 == 0 ? currentTbId / 5 : currentTbId / 5 + 1;
//        StringBuilder sb = new StringBuilder(tbNamePrefix + "_" + currentTbId + ","); // tbName
//        sb.append(ts).append(','); // ts
//        sb.append(current[currentRow % 5]).append(','); // current
//        sb.append(voltage[currentRow % 5]).append(','); // voltage
//        sb.append(phase[currentRow % 5]).append(','); // phase
//        sb.append(location[currentRow % 5]).append(','); // location
//        sb.append(groupId); // groupID
        //每一秒产生一条数据
        long ts = DateTime.now().getTime();
        int size = tagDataList.size();
        DemoTagData tagData = tagDataList.get(currentTbId);
        StringBuilder sb = new StringBuilder();
        sb.append(ts).append(',');
        sb.append(tagData.getMockVal(TagType.DOUBLE)).append(',');
        sb.append(tagData.getQ()).append(',');

        return sb.toString();
    }

    public String nextDate(DateTime dt) {
        //每一秒产生一条数据
        long ts = dt.getTime();
        int size = tagDataList.size();

        DemoTagData tagData = tagDataList.get(currentTbId);
        if(currentTbId == tagDataList.size() - 1){
            return null;
        }
        String tbName = tagData.getTagId();
        StringBuilder sb = new StringBuilder();
        sb.append(tbName).append(',');
        sb.append(ts).append(',');
        sb.append(tagData.getMockVal(TagType.DOUBLE)).append(',');
        sb.append(tagData.getQ());

        return sb.toString();
    }
}
